<template>
    <div class="app-container">
        <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="套餐名称">
                <el-input v-model="queryParams.name" placeholder="请输入套餐名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        -->
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="postList">
            <el-table-column label="商品名称" align="center" prop="name">
                <template slot-scope="scope">
                    <el-tag>{{ scope.row.name }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="套餐图片" align="center" prop="imageUrl">
                <template slot-scope="scope">
                    <el-image style="width: 100px; height: 100px" :src="scope.row.imageUrl"
                        :preview-src-list="[scope.row.imageUrl]"></el-image>
                </template>
            </el-table-column>
            <el-table-column label="套餐描述" align="center" prop="description" />
            <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createdAt) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit"
                        @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete"
                        @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 添加或修改岗位对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="套餐名称" prop="name">
                    <!-- <el-radio-group v-model="form.name">
                        <el-radio label="1">200面值</el-radio>
                        <el-radio label="2">300面值</el-radio>
                        <el-radio label="3">500面值</el-radio>
                    </el-radio-group> -->
                    <el-input v-model="form.name" placeholder="请输入套餐名称" />
                </el-form-item>
                <el-form-item label="套餐图片" prop="imageUrl">
                    <FileUpload ref="FileUpload" @inputFile="FileUploadValChange" :value="FileUploadVal" :limit="1"
                        :fileType="fileType">
                    </FileUpload>
                </el-form-item>
                <el-form-item label="套餐描述" prop="description">
                    <el-input v-model="form.description" type="textarea" placeholder="请输入商品描述" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { comboListApi, comboInsertApi, comboUpdateApi } from "@/api/index"
export default {
    name: "Post",
    dicts: [],
    data() {
        return {
            fileType: ['png', 'jpg', 'jpeg'], // 文件类型
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 岗位表格数据
            postList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                name: undefined,
                // pageSize: 10,
                // postCode: undefined,
                // postName: undefined,
                // status: undefined
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    { required: true, message: "套餐名称不能为空", trigger: "blur" }
                ],
                imageUrl: [
                    { required: true, message: "套餐图片不能为空", trigger: "blur" }
                ],
                description: [
                    { required: true, message: "套餐描述不能为空", trigger: "blur" }
                ]

            },
            FileUploadVal: '',
            baseUrl: process.env.VUE_APP_BASE_API,
        }
    },
    created() {
        this.getList()
    },
    methods: {
        FileUploadValChange(val) {
            console.log(val, 'FileUploadValChange');
            this.FileUploadVal = val;
            this.form.imageUrl = val[0].url; // 假设只上传一个文件
        },
        /** 查询产品列表 */
        getList() {
            this.loading = true
            comboListApi(this.queryParams).then(response => {
                console.log(response, 'response');
                this.postList = response.data;
                // this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                name: undefined,
                imageUrl: undefined,
                description: undefined,
            }
            this.resetForm("form")
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                name: undefined,
            }
            this.resetForm("queryForm")
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.postId)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = "添加套餐";
            this.FileUploadVal = [];
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            this.form = JSON.parse(JSON.stringify(row))
            this.open = true
            this.title = "修改商品";
            this.FileUploadVal = [
                {
                    url: row.imageUrl,
                    name: row.imageUrl.split('/').pop() // 获取文件名
                }
            ]
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    const params = this.form;
                    console.log(params, 'params');
                    if (this.form.id != undefined) {
                        comboUpdateApi(params).then(response => {
                            this.$modal.msgSuccess("修改成功")
                            this.open = false
                            this.getList()
                        })
                    } else {
                        comboInsertApi(params).then(response => {
                            this.$modal.msgSuccess("新增成功")
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.$modal.confirm('是否确认删除该套餐吗').then(function () {
                return comboUpdateApi({
                    id: row.id,
                    status: 0
                })
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess("删除成功")
            }).catch(() => { })
        },
    }
}
</script>
